<template>
	<view class="search">
    <input type="text" class="searchplace"  placeholder="请输入目的地" @input="handleipt">
    <view class="lists">
      <view  v-for="(item,index) in suggestions" :key="item.id">
        <view class="item" @click="godest(index)">
          <view class="left tnowrap">
            <view class="title tnowrap">
            {{item.title}}
            </view>
            <view class="addr tnowrap">
            {{item.addr}}
            </view>
          </view>
          <div class="right">
            <view class="go">去这里</view>
          </div>
        </view>
      </view>
    </view>
    
  </view>
</template>

<script>
  import store from '../../store/store.js'
 const QQMapWX = require('../../libs/txmapv2');
  // 实例化API核心类
const qqmapsdk = new QQMapWX({
    key: 'OMYBZ-YFSK2-DD5UX-CZS76-FM6Z2-KYFTF' 
  });
	export default{
    data(){
      return {
        keyword:"",
        destarea:{},
        suggestions:[]
      }
    },
    computed: {
        city () {
          return this.$store.state.destareainfo.city
        }
      },
    methods:{
      // 输入处理
      handleipt(e){
        this.keyword = e.detail.value
        var _that = this
        qqmapsdk.getSuggestion({
              //获取输入框值并设置keyword参数
              keyword: e.detail.value, //用户输入的关键词，可设置固定值,如keyword:'KFC'
              region:'成都', //设置城市名，限制关键词所示的地域范围，非必填参数
              success: function(res) {//搜索成功后的回调
                _that.suggestions = []
                for (var i = 0; i < res.data.length; i++) {
                  _that.suggestions.push({ // 获取返回结果，放到sug数组中
                    title: res.data[i].title,
                    id: res.data[i].id,
                    addr: res.data[i].address,
                    city: res.data[i].city,
                    district: res.data[i].district,
                    latitude: res.data[i].location.lat,
                    longitude: res.data[i].location.lng
                  });
                }
              },
              fail: function(error) {
                console.error(error);
              },
              complete: function(res) {
                // console.log(res.data);
              }
            });
      },
      // 将目的地存入vuex 以及跳转到主页
      godest(index){
        store.commit("changedestinfo",this.suggestions[index])
        uni.switchTab({
        	url: '/pages/index/index'
        });
      }
    }
  }
</script>

<style scoped>
  .item{
    line-height: 30px;
    border-bottom: 1px solid #ddd;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .left{
    flex: 1;
  }
  .right{
    width: 80px;
    background-color: orangered;
    text-align: center;
    line-height: 30px;
    color:#fff;
  }
  .addr{
    font-size: 12px;
    color: #ccc;
  }
  .searchplace{
    border: 1px solid rgba(0, 0, 0, .4);
    padding: 10px;
  }
</style>